import time

import requests
# from django.http import JsonResponse
# from indexs.models import SKU
from django.http import JsonResponse
# from video.models import VideoData, VideoGoodsCategory, AudioData
# from django.views import View
from users.models import User

"""文件存储"""

def request_to(img, file, chush, ord_img):
    print(ord_img)
    baseurl = 'http://134.175.230.195:5555'

    user = 'qqo234cune121k3232c9r'
    date = '12fbdvhdv84843498ggfd'

    keys = encode_key(user, date)

    if chush == 0:
        url = baseurl + '/push_image/skus'
    elif chush == 1:
        url = baseurl + '/push_video'
    elif chush == 2:
        url = baseurl + '/push_audio'
    else:
        url = '/'

    data = {
        'sku':2021,
        'sku_img': 2021,
        'user': f'{time.time()}',
        'status': '.mp3' if chush == 2 else '.mp4', # 没有指定格式 浏览器会下载
        'ord_img': ord_img ,
        'keys': keys
    }
    files = {
        "video": file,
        'image': img,
        'audio': file
    }

    response = requests.post(url=url, data=data, files=files).content.decode()
    return response

# .jpg

# class Push_Video(View):
#
#     def post(self, request):
#         file_img = request.FILES.get('image', None)
#         file_video = request.FILES.get('video', None)
#         author = request.POST.get('author', None)
#         cate = request.POST.get('cate', None)
#         name = request.POST.get('name', None)
#
#         # 校验
#         if file_img is None or file_video is None or name is None:
#             return JsonResponse({'code': "400"})
#
#         try:
#             cate = VideoGoodsCategory.objects.get(id=cate)
#         except:
#             return JsonResponse({'code': "400"})
#         try:
#             author = User.objects.get(id=author)
#         except:
#             return JsonResponse({'code': "400"})
#
#         # 分类
#         img = request_to(file_img, '', 0)
#         video_url = request_to('', file_video, 1)
#
#         try:
#             VideoData.objects.create(name=name, cate=cate, default_img=img, video_url=video_url, author=author)
#
#         except Exception as e:
#             print(e)
#             return JsonResponse({'code': '文件写入失败'})
#
#         return JsonResponse({'code': '200'})
#
#
# class PushAudio(View):
#
#     def post(self, request):
#         file_img = request.FILES.get('image', None)
#         b_img = request.FILES.get('b_image', None)
#         file_audio = request.FILES.get('audio', None)
#         author = request.POST.get('author', None)
#         cate = request.POST.get('cate', None)
#         name = request.POST.get('name', None)
#
#         # 校验
#         if file_img is None or file_audio is None or name is None or b_img is None:
#             return JsonResponse({'code': "400"})
#
#         try:
#             cate = VideoGoodsCategory.objects.get(id=cate)
#         except:
#             return JsonResponse({'code': "400"})
#
#         # 分类
#         img = request_to(file_img, file_audio, 0)
#         b_img = request_to(b_img, file_audio, 0)
#         audio_url = request_to(file_img, file_audio, 2)
#
#         try:
#             AudioData.objects.create(name=name, cate=cate, default_img=img, background_img=b_img ,audio_url=audio_url, author=author)
#
#         except Exception as e:
#             print(e)
#             return JsonResponse({'code': '文件写入失败'})
#
#         return JsonResponse({'code': '200'})
#
#



def encode_key(user,date):
    key = 'ehr{#@}d{deec{21hx8}{e2ccr3-_3+3dc}[ec[]eeedc'

    data = "{'user':'"+ user +"','date':'"+ date +"'}"
    if len(data) % 2 != 0:
        data += '6'
        d1 = data[0:len(data):2][::-1]
        d2 = data[1:len(data):2][::-1]
        data_len = (len(data)+1)//2
        print(d2)
    else:
        # data += '6'
        d1 = data[0:len(data) + 1:2][::-1]
        d2 = data[1:len(data) + 1:2][::-1]
        data_len = len(data)//2


    keys = ''
    for i in range(data_len):
        keys += f'{d1[i] + key[i] + d2[i] + key[-i]}'
    if data[-1]=='6':
        keys += '6'

    return keys